package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.Brand;
import com.cskaoyan.bean.BrandExample;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.service.BrandService;
import com.cskaoyan.utils.CommonUtil;
import com.cskaoyan.utils.Constant;
import com.cskaoyan.utils.ErrState;
import com.cskaoyan.utils.MarketPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Jcy
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    /**
     * 商场管理
     * 品牌制造商初始化
     *
     * @param id
     * @param name
     * @param param
     * @return
     */
    @Override
    public BasePageData list(Integer id, String name, BaseParam param) {
        MarketPageUtil.startPage(param);
        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (!CommonUtil.isNull(id)){
            criteria.andIdEqualTo(id);
        }
        if (!CommonUtil.isNull(name)){
            criteria.andNameLike("%" + name + "%");
        }
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Brand> brands = brandMapper.selectByExample(example);
        return BasePageData.ok(brands);
    }

    /**
     * 添加
     * @param brand
     * @return
     */
    @Override
    public int create(Brand brand) {
        if (CommonUtil.isNull(brand.getName()) || CommonUtil.isNull(brand.getDesc()) || CommonUtil.isNull(brand.getFloorPrice())){
            return ErrState.PARAM_ERROR.getErrno();
        }
        try {
            int affectRows = brandMapper.insertSelective(brand);
            if (affectRows == 1){
                return Constant.SUCCESS;
            }
        }catch (Exception e){
            return ErrState.PARAM_ERROR.getErrno();
        }
        return Constant.FAIL;
    }

    /**
     * 删除
     * @param brand
     * @return
     */
    @Override
    public int delete(Brand brand) {
        brand.setUpdateTime(new Date());
        brand.setDeleted(true);
        int affectRows = brandMapper.updateByPrimaryKeySelective(brand);
        if (affectRows == 1){
            return Constant.SUCCESS;
        }
        return Constant.FAIL;
    }

    /**
     * 修改
     * @param brand
     * @return
     */
    @Override
    public int update(Brand brand) {
        brand.setAddTime(null);
        brand.setUpdateTime(new Date());
        int affectRows = brandMapper.updateByPrimaryKeySelective(brand);
        if (affectRows == 1){
            return Constant.SUCCESS;
        }
        return Constant.FAIL;
    }
    /*微信端*/
    @Override
    public BasePageData listInWechat(BaseParam param) {
        param.setSort(Constant.SORT);
        param.setOrder(Constant.ORDER);
        MarketPageUtil.startPage(param);
        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Brand> brands = brandMapper.selectByExample(example);
        return BasePageData.ok(brands);
    }

    @Override
    public Brand detail(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

}
